<?php
defined( '_JEXEC' ) or die( 'Restricted access' ); 
JLoader::register('linioFunctions',JPATH_COMPONENT.DS.'helpers'.DS.'functions.php');
class SiteMapGenerator  {	
	const XML_HEADER = '<?xml version="1.0" encoding="UTF-8"?>';
	private $lastmod;
	private $aStdCategories;	
	
	function __construct(){
		$this->lastmod = date("Y-m-d");
		$this->aStdCategories = array();	
	}
	
	function setAStdCategories($aStdCategories){
		$this->aStdCategories = $aStdCategories;		
	}
	
	function getAStdCategories(){
		return $this->aStdCategories;		
	}

	function setLastmod($lastmod){
		$this->lastmod = $lastmod;
	}
		
	function getLastmod(){
		return $this->lastmod;
	}
	
	public function creteSiteMap(){
		$i=0;
		$protocol = strtolower(substr($_SERVER["SERVER_PROTOCOL"],0,strpos( $_SERVER["SERVER_PROTOCOL"],'/'))).'://';
		$baseUrl = $protocol.$_SERVER["HTTP_HOST"];
		$attrs['xmlns'] = 'http://www.sitemaps.org/schemas/sitemap/0.9';
		$urlset = new JSimpleXMLElement('urlset',$attrs);
		foreach ($this->aStdCategories as $cat) {
			// crear la url
			$loc = linioFunctions::createMenuCategoryURL($cat->id,$cat->category,'MX',$baseUrl);
			//crear el nodo "url"
			$url = $urlset->addChild('url');
			//crear los nodos hijos.
			$this->getUrlTag($loc, $url);
			if($i++ >= 1000) break;
		}		
		return self::XML_HEADER.$urlset->toString();
	}

	/**
	 * 
	 * Recupera el tag con los datos de la URL. 
	 * @param string $loc
	 * @param string $xmlElement
	 * @param decimal $priority
	 * @param string $changefreq
	 */
	public function getUrlTag($loc,$xmlElement,$priority='0.5',$changefreq='weekly'){		
		$l = $xmlElement->addChild('loc');
		$l->setData(htmlentities($loc));
		$p = $xmlElement->addChild('priority');
		$p->setData($priority);
		$c = $xmlElement->addChild('changefreq');
		$c->setData($changefreq);	
		$l = $xmlElement->addChild('lastmod');
		$l->setData($this->lastmod);		
		return $xmlElement;
	}

}
?>